package com.xuelang.pipeline.app.task.impl;

import com.xuelang.pipeline.app.resource.ConfigMapService;
import com.xuelang.pipeline.app.resource.request.ConfigMapRequest;
import com.xuelang.pipeline.app.task.TaskInstanceService;
import com.xuelang.pipeline.app.task.converter.TaskInstanceToDTOConverter;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.pipeline.enums.ApproveEnum;
import com.xuelang.pipeline.client.task.dto.TaskInstanceDTO;
import com.xuelang.pipeline.domain.pipeline.Pipeline;
import com.xuelang.pipeline.domain.pipeline.PipelineInstance;
import com.xuelang.pipeline.domain.pipeline.service.PipelineDomainService;
import com.xuelang.pipeline.domain.pipeline.service.PipelineInstanceDomainService;
import com.xuelang.pipeline.domain.resource.Cluster;
import com.xuelang.pipeline.domain.resource.Environment;
import com.xuelang.pipeline.domain.resource.Space;
import com.xuelang.pipeline.domain.resource.service.ClusterDomainService;
import com.xuelang.pipeline.domain.resource.service.EnvironmentDomainService;
import com.xuelang.pipeline.domain.resource.service.SpaceDomainService;
import com.xuelang.pipeline.domain.task.TaskInstance;
import com.xuelang.pipeline.domain.task.service.TaskInstanceDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>任务实例服务接口实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-25 10:03
 **/
@Service
@Slf4j
public class TaskInstanceServiceImpl implements TaskInstanceService {

    @Autowired
    private TaskInstanceDomainService taskInstanceDomainService;
    @Autowired
    private PipelineDomainService pipelineDomainService;
    @Autowired
    private PipelineInstanceDomainService pipelineInstanceDomainService;
    @Autowired
    private EnvironmentDomainService environmentDomainService;
    @Autowired
    private ConfigMapService configMapService;
    @Autowired
    private SpaceDomainService spaceDomainService;
    @Autowired
    private ClusterDomainService clusterDomainService;

    @Override
    public List<TaskInstanceDTO> queryByPipelineInstanceId(String pipelineInstanceCode) {
        List<TaskInstance> listResult = taskInstanceDomainService.queryByPipelineInstanceCode(pipelineInstanceCode);
        if (null == listResult) {
            listResult = new ArrayList<>();
        }
        return listResult.stream().map(TaskInstanceToDTOConverter.INSTANCE::toDTO).collect(Collectors.toList());
    }

    @Override
    public TaskInstanceDTO queryByTaskCode(String taskInstanceCode) {
        TaskInstance taskInstance = taskInstanceDomainService.queryByTaskCode(taskInstanceCode);
        return TaskInstanceToDTOConverter.INSTANCE.toDTO(taskInstance);
    }

    @Override
    public void add(TaskInstanceDTO taskInstance) {
        taskInstanceDomainService.add(TaskInstanceToDTOConverter.INSTANCE.fromDTO(taskInstance));
    }

    @Override
    public void update(TaskInstanceDTO taskInstance) {
        taskInstanceDomainService.update(TaskInstanceToDTOConverter.INSTANCE.fromDTO(taskInstance));
    }

    @Override
    public void removeByIds(List<String> idList) {
        taskInstanceDomainService.removeByIds(idList);
    }

    @Override
    public void removeByPipelineInstanceIdList(List<String> pipelineInstanceCodeList) {
        taskInstanceDomainService.removeByPipelineInstanceCodeList(pipelineInstanceCodeList);
    }

    @Override
    public void removeByTaskUuidList(List<String> taskUuidList) {
        taskInstanceDomainService.removeByTaskUuidList(taskUuidList);
    }

    @Override
    public Boolean approveTaskInstance(String taskInstanceCode, String type) {
        TaskInstance taskInstance = taskInstanceDomainService.queryByTaskCode(taskInstanceCode);
        PipelineInstance pipelineInstance = pipelineInstanceDomainService.queryByCode(taskInstance.getPipelineInstanceCode());
        Pipeline pipeline = pipelineDomainService.query(pipelineInstance.getPipelineCode());
        Environment environment = environmentDomainService.query(pipeline.getEnvironmentUuid());
        String uniqueCode = pipeline.getComponentLabel() + "-" + environment.getCode();
        String configmapName = "approve-" + uniqueCode;

        Space space = spaceDomainService.query(environment.getSpaceUuid());
        Cluster cluster = clusterDomainService.query(space.getClusterUuid());
        ConfigMapRequest request = new ConfigMapRequest();
        request.setKubeConfig(cluster.getKubeConfig());
        request.setConfigMapName(configmapName);
        request.setNamespaceName(Constant.BUILD_NAMESPACE);
        request.setData(getReplaceMap(type));
        ApiResult<Boolean> result = configMapService.saveOrUpdate(request);
        return result.getData();
    }

    @Override
    public List<String> queryBuildOutput(String pipelineInstanceCode, String taskInstanceCode) {
        return taskInstanceDomainService.queryBuildOutput(pipelineInstanceCode, taskInstanceCode);
    }


    private Map<String, String> getReplaceMap(String type) {
        Map<String, String> replaceMap = new HashMap<>();
        replaceMap.put("status", ApproveEnum.fromKey(type).getValue());
        return replaceMap;
    }
}
